package scantask.tests;

import java.util.Set;
import java.util.Vector;

import junit.framework.TestCase;
import scantask.containers.Benchmark;
import scantask.containers.Coordinate;
import scantask.containers.InformationObject;
import scantask.containers.WeekDate;
import scantask.containers.exceptions.InvalidCoordinateValueException;
import scantask.containers.exceptions.InvalidWeekNumberException;
import scantask.containers.exceptions.InvalidYearException;
import scantask.server.administrator.Administrator;
import scantask.server.domainLayer.Controller;
import scantask.server.domainLayer.Exceptions.AddBenchmarkException;
import scantask.server.domainLayer.Exceptions.ControllerException;
import scantask.server.domainLayer.Exceptions.GetInformativeDataException;
import scantask.server.domainLayer.Exceptions.GetUserBenchmarksException;
import scantask.server.domainLayer.Exceptions.GetValidDatesException;
import scantask.server.domainLayer.Exceptions.LoginException;


public class UnitTests extends TestCase{
	public static int count = (int)(100*(Math.random()));
	private scantask.server.domainLayer.Controller _serverControl;
	private scantask.client.domainLayer.Controller _clientControl;
	public UnitTests(){
		this._serverControl = new Controller();
		this._clientControl = new scantask.client.domainLayer.Controller();
	}
	public void testServerLogin1(){
		try {
			this._serverControl.login("unidentified", "unidentified");
			assertTrue(false);
		} catch (LoginException e) {
			assertTrue(true);
			e.printStackTrace();
		}
	}
	public void testServerLogin2(){
		try {
			String encrypted = Administrator.encryptPassword("kornberg");
			this._serverControl.login("itai",encrypted);
			assertTrue(true);
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}
	public void testServerLogin3(){
		try {
			this._serverControl.login("itai", "wrongPassword");
			assertTrue(false);
		} catch (LoginException e) {
			assertTrue(true);
			e.printStackTrace();
		}
	}
//	public void testServerLogout1(){
//		//cant test that
//	}
//	public void testServerLogout2(){
//		//cant test that
//	}
//	public void testServerLogout3(){
//		//cant test that
//	}
//	public void testServerGetDefaultLocation1(){
//		//already in login
//	}
//	public void testServerGetDefaultLocation2(){
//		//already in login
//	}
	public void testServerGetUserBenchmarks1(){
		try {
			Vector<Benchmark> v =this._serverControl.getUserBenchmarks("unidentified");
			assertTrue(false);
			
		} catch (GetUserBenchmarksException e) {
			assertTrue(true);
			e.printStackTrace();
		}
	}
//	public void testServerGetUserBenchmarks2(){
//		try {
//			String encrypted = Administrator.encryptPassword("kornberg");
//			this._serverControl.login("itai",encrypted);
//		} catch (LoginException e) {
//			e.printStackTrace();
//		}
//		try {
//			this._serverControl.addBenchmark("itai", "beer Sheva", new Coordinate(5, 5));
//		} catch (AddBenchmarkException e) {
//			e.printStackTrace();
//		} catch (InvalidCoordinateValueException e) {
//			e.printStackTrace();
//		}
//		try {
//			Vector<Benchmark> v = this._serverControl.getUserBenchmarks("itai");
//			for(Benchmark b:v){
//				if(b.getName().equals("beer Sheva")){
//					assertTrue(true);
//					return;
//				}
//			}
//			assertTrue(false);
//		} catch (GetUserBenchmarksException e) {
//			assertTrue(false);
//			e.printStackTrace();
//		}
//	}
	
	public void testServerGetInformativeData1(){
		try {
			Vector<InformationObject> v = this._serverControl.getInformativeData("unidentified", new WeekDate(1, 2010));
			assertTrue(v.size()==0);
			
		} catch (InvalidWeekNumberException e) {
			e.printStackTrace();
		} catch (InvalidYearException e) {
			e.printStackTrace();
		} catch (GetInformativeDataException e) {
			e.printStackTrace();
		}
	}
	
	public void testServerGetInformativeData2(){
		try {
			Vector<InformationObject> v = this._serverControl.getInformativeData("guy", new WeekDate(32, 2010));
			assertTrue(v.size()>0);
		} catch (InvalidWeekNumberException e) {
			e.printStackTrace();
		} catch (InvalidYearException e) {
			e.printStackTrace();
		} catch (GetInformativeDataException e) {
			e.printStackTrace();
		}
	}

	public void testServerGetInformativeData3(){
		try {
			Vector<InformationObject> v = this._serverControl.getInformativeData("guy", new WeekDate(1, 2010));
			assertTrue(v.size()==0);
			
		} catch (InvalidWeekNumberException e) {
			e.printStackTrace();
		} catch (InvalidYearException e) {
			e.printStackTrace();
		} catch (GetInformativeDataException e) {
			e.printStackTrace();
		}
	}
	public void testServerGetInformativeData4(){
		try {
			Vector<InformationObject> v = this._serverControl.getInformativeData("guy", new WeekDate(54, 2010));
			assertTrue(false);
			
		} catch (InvalidWeekNumberException e) {
			assertTrue(true);
			e.printStackTrace();
		} catch (InvalidYearException e) {
			e.printStackTrace();
		} catch (GetInformativeDataException e) {
			e.printStackTrace();
		}
	}
	
	public void testServerAddBenchmark1 (){
		try {
			String encrypted = Administrator.encryptPassword("kornberg");
			this._serverControl.login("itai", encrypted);
			this._serverControl.addBenchmark("itai", "a", new Coordinate(190, 2));
			assertTrue(false);
			this._serverControl.logout("itai");
		} catch (AddBenchmarkException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			assertTrue(true);
			e.printStackTrace();
		} catch (LoginException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void testServerAddBenchmark2 (){
		try {
			String encrypted = Administrator.encryptPassword("kornberg");
			this._serverControl.login("itai", encrypted);
			this._serverControl.addBenchmark("itai", "benchmark"+(this.count++), new Coordinate(3, 2));
			assertTrue(true);
			this._serverControl.logout("itai");
		} catch (AddBenchmarkException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}

	public void testServerAddBenchmark3 (){
		try {
			String encrypted = Administrator.encryptPassword("kornberg");
			this._serverControl.login("itai", encrypted);
			this._serverControl.addBenchmark("itai", "a", new Coordinate(3, 2));
			assertTrue(false);
			this._serverControl.logout("itai");
		} catch (AddBenchmarkException e) {
			assertTrue(true);
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}
	public void testServerAddBenchmark4 (){
		try {
			String encrypted = Administrator.encryptPassword("kornberg");
			this._serverControl.login("itai", encrypted);
			this._serverControl.addBenchmark("itai", "", new Coordinate(3, 2));
			assertTrue(false);
		} catch (AddBenchmarkException e) {
			assertTrue(true);
			e.printStackTrace();
		} catch (InvalidCoordinateValueException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}
	
	public void testServerGetValidDates1 (){
		try {
			this._serverControl.getValidDates("unidentified");
		} catch (GetValidDatesException e) {
			assertTrue(true);
			e.printStackTrace();
		}
	}
	public void testServerGetValidDates2 (){
		try {
			String encrypted = Administrator.encryptPassword("kornberg");
			this._serverControl.login("itai", encrypted);
			Set<WeekDate> s = this._serverControl.getValidDates("itai");
			assertTrue(s.size()>0);
			this._serverControl.logout("itai");
		} catch (GetValidDatesException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}
	
	public void testClientViewHistory1 (){
		try {
		//	String encrypted = Administrator.encryptPassword("segev");
			this._clientControl.login("itai", "kornberg");
			this._clientControl.viewHistory(2010, 54);
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (InvalidWeekNumberException e) {
			assertTrue(true);
			e.printStackTrace();
		} catch (InvalidYearException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (ControllerException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}
	public void testClientViewHistory2 (){
		try {
		//	String encrypted = Administrator.encryptPassword("segev");
			this._clientControl.login("itai", "kornberg");
			Vector<InformationObject> v = this._clientControl.viewHistory(2010, 32);
			assertTrue(v.size()>0);
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (InvalidWeekNumberException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (InvalidYearException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (ControllerException e) {
			assertTrue(false);
			e.printStackTrace();
		}
	}
	public void testClientGetDataTypes (){
		try {
		//	String encrypted = Administrator.encryptPassword("segev");
			this._clientControl.login("itai", "kornberg");
			Set<String> s = this._clientControl.getDataTypes();
			assertTrue(s.size()>0);
		} catch (LoginException e) {
			assertTrue(false);
			e.printStackTrace();
		} catch (ControllerException e) {
			assertTrue(false);
			e.printStackTrace();
		} 
	}
}
